﻿using RpcCentral.Collect.Model;
using RpcCentral.DAL;
using RpcCentral.Model.DB;
using WeDonekRpc.CacheClient.Interface;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace RpcCentral.Collect.Collect
{
    /// <summary>
    /// 容器管理
    /// </summary>
    internal class ContainerCollect : IContainerCollect
    {
        private readonly IRemoteServerDAL _RemoteServer;
        private readonly IContainerDAL _Container;
        private readonly ICacheController _Cache;
        private readonly ITransactionService _Transaction;
        public ContainerCollect (IRemoteServerDAL remoteServer,
            ICacheController cache,
            ITransactionService transaction,
            IContainerDAL container)
        {
            this._Transaction = transaction;
            this._Cache = cache;
            this._RemoteServer = remoteServer;
            this._Container = container;
        }

        public long RegContainer (ContrainerLogin param)
        {
            string key = string.Concat("Con_", param.ContrainerId, "_", param.ContGroupId);
            if (this._Cache.TryGet(key, out long id))
            {
                return id;
            }
            id = this._Container.Find(param.ContGroupId, param.ContrainerId);
            if (id == 0)
            {
                id = this._AddContrainer(param);
            }
            _ = this._Cache.Set(key, id);
            return id;
        }
        private long _AddContrainer (ContrainerLogin param)
        {
            if (param.LocalMac == null)
            {
                param.LocalMac = Tools.GetRandomMac();
            }
            Contrainer add = new Contrainer
            {
                ContrainerId = param.ContrainerId,
                VirtualMac = param.LocalMac,
                GroupId = param.ContGroupId
            };
            RemoteServerConfig server = new RemoteServerConfig
            {
                SystemType = param.SystemType,
                ServerIp = param.ServerIp,
                ServiceState = RpcServiceState.待启用,
                IsContainer = true,
                GroupId = param.GroupId,
                ServiceType = param.ServiceType,
                RegionId = param.RegionId,
                HoldRpcMerId = param.RpcMerId,
                PublicKey = Tools.NewGuid().ToString("N").Substring(Tools.GetRandom(0, 19), 12),
                ServerPort = param.ServerPort,
                ServerMac = add.VirtualMac,
                IsEnablePipe = false,
                ContainerGroupId = add.GroupId,
                ConIp = param.ServerIp,
                ServerName = string.Concat("容器_", param.SystemName, "节点"),
                RemoteIp = param.ServerIp,
                RemotePort = param.ServerPort
            };
            using (ILocalTransaction tran = this._Transaction.ApplyTran())
            {
                server.ContainerId = this._Container.Reg(add);
                _ = this._RemoteServer.Add(server);
                tran.Commit();
            }
            return server.ContainerId.Value;
        }
    }
}
